package com.liuxm.user.controller;

import cn.hutool.core.util.RandomUtil;
import com.liuxm.user.pojo.User;
import com.liuxm.user.service.UserService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    UserService userService;

    @Value("${server.port}")
    private String value;

    @GetMapping("{username}")
    public User queryByUsername(@PathVariable String username, HttpServletRequest request) {
        System.out.println("当前调用的端口==================>" + value);
        String name = request.getHeader("name");
        System.out.println("获取到的请求头信息===============>" + name);
        sleep();
        return userService.queryByUsername(username);
    }

    private void sleep() {
        int randomInt = RandomUtil.randomInt(5000);
        randomInt = 5000;
        try {
            Thread.sleep(randomInt);
            System.out.println("休眠时间===================>" + randomInt);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}